﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.customer.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.hh.bss.base.*;
import com.hh.bss.common.SystemConstant;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.config.Environment;
import com.hh.bss.customer.dao.*;
import com.hh.bss.customer.entity.*;
import com.hh.bss.customer.service.*;
import com.hh.bss.customer.vo.query.*;
import com.hh.bss.util.*;




@Service
@Transactional
public class CustomerContractAttachmentServiceImpl extends BaseBusinessService<CustomerContractAttachment,java.lang.Integer>  implements CustomerContractAttachmentService<CustomerContractAttachment,java.lang.Integer>{

	private CustomerContractAttachmentDao customerContractAttachmentDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setCustomerContractAttachmentDaoImpl(CustomerContractAttachmentDao dao) {
		this.customerContractAttachmentDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.customerContractAttachmentDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(CustomerContractAttachmentQuery query) {
		return customerContractAttachmentDao.findPage(query);
	}
	public void saveFile(MultipartHttpServletRequest multipartRequest,
			HttpServletRequest request,
			CustomerContractAttachment customerContractAttachment) {
		// TODO Auto-generated method stub
		
			// TODO Auto-generated method stub
			  SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");     
			  MultipartFile multipartFile = multipartRequest.getFile("uploadFile");
		       /**构建文件保存的web目录**/     
		       
		       //把公司id也加到目录里
		       String fileWebPathDir = Environment.UPLOADFILEDIR+"/"+SystemConstant.CUSTCONTRACTDIR+"/"+dateformat.format(new Date())+"/"+customerContractAttachment.getId();      
		       /**得到文件保存目录的真实路径**/     
		       String fileRealPathDir = request.getSession().getServletContext().getRealPath(fileWebPathDir);     
		       /**根据真实路径创建目录**/     
		       File fileSaveDir = new File(fileRealPathDir);     
		       if(!fileSaveDir.exists()) {    
		           fileSaveDir.mkdirs();     
		       }   
		       /**页面控件的文件流**/     
		       //MultipartFile multipartFile = multipartRequest.getFile("fileRes");      			       			      			       
		       /**上传文件名称**/     
		       String uploadFileName = multipartFile.getOriginalFilename();//    
		         
		       
		       /**获取文件的后缀**/     
		       String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".")+1);     
		       String preName = multipartFile.getOriginalFilename().substring(0,multipartFile.getOriginalFilename().indexOf("."));
		       // 文件类型   
		         
		       // 获取文件大小，以kB为单位   
		       long size = multipartFile.getSize();   
		       Double fSize =Math.round(size/1024.0*100)/100.0;   
		       String fileSize = Double.toString(fSize)+"KB";
		       if (fSize==0) fSize=new Double(1);
		       /**拼成完整的文件保存路径加文件**/     
		     //重新命名
		       if(!StringHelper.isEmpty(suffix))uploadFileName=RandomStringUtil.getString(8)+"."+suffix;
		       else uploadFileName=RandomStringUtil.getString(8);
		       
		       String fileName = fileRealPathDir + File.separator   + uploadFileName;     
		       
		       File file = new File(fileName);      
		          
		       try {     
		           multipartFile.transferTo(file);  //将文件写入到指定的目录   
		       } catch (IllegalStateException e) {     
		           e.printStackTrace();     
		       } catch (IOException e) {            
		           e.printStackTrace();     
		       }    
		       customerContractAttachment.setAttachmentFile(fileWebPathDir+"/"+ uploadFileName);
		       //如果没有填文件名称，则以文件名处理
		       
		       if (StringHelper.isEmpty(customerContractAttachment.getAttachmentName())) customerContractAttachment.setAttachmentName(preName);	       
		       customerContractAttachment.setAttachmentFilesize(fSize.intValue());	       
		       customerContractAttachment.setAttachmentFiletype(suffix);
		
	}
	
}
